package com.wuseguang.springcloud.service.impl;

import com.wuseguang.springcloud.dao.jdbc.OiCountdownJdbc;
import com.wuseguang.springcloud.dao.repository.OiCountdownRepository;
import com.wuseguang.springcloud.entity.OiCountdown;
import com.wuseguang.springcloud.service.OiCountdownService;
import com.wuseguang.springcloud.util.DateUtil;
import com.wuseguang.springcloud.util.PaginationUtil;
import com.wuseguang.springcloud.util.PagingTool;
import com.wuseguang.springcloud.util.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 事件倒计时 服务实现类
 * </p>
 *
 * @author 
 * @since 2020-06-30
 */
@Service
public class OiCountdownServiceImpl  implements OiCountdownService {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private OiCountdownJdbc oiCountdownJdbc;
    @Autowired
    private OiCountdownRepository oiCountdownRepository;

    @Override
    public Result addOiCountdown(OiCountdown oiCountdown) {
        logger.info("添加倒计时事件");
        //班级id
        String clssIds[] = oiCountdown.getClassIds().split(",");

        //添加时间
        oiCountdown.setAddTime(new Date());
        //循环班级id
        for (int i = 0;i<clssIds.length;i++) {
            Integer classId = Integer.valueOf(clssIds[i]);
            //查询置顶的信息
            OiCountdown oiCountdownTwo = oiCountdownRepository.findByClassIdAndTopping(classId,1);
            //不为空
            if(!StringUtils.isEmpty(oiCountdownTwo)){
                oiCountdownTwo.setTopping(0);
                oiCountdownRepository.save(oiCountdownTwo);
            }
            //查询置顶的信息
            logger.info("classId ={}",classId);
            oiCountdown.setClassId(classId);
            //不修改为0程序会以为修改数据库数据
            oiCountdown.setId(0);
            oiCountdownRepository.save(oiCountdown);

        }

        return new Result();
    }

    @Override
    public Result findClassOiCountdown(Integer classId, Integer indexNo, Integer rows) {
        logger.info("查询倒计时事件");
        //根据班级查询事件记录
        List<OiCountdown> list = oiCountdownRepository.findByClassId(classId);
        //循环事件
        for (OiCountdown oiCountdown :list) {
            //获取今天是信息几
            oiCountdown.setWeek(DateUtil.getWeeks(DateUtil.parseData(oiCountdown.getEventTime(),"yyyy-MM-dd HH:mm:ss")));
        }

       /* //创建分页工具
        PagingTool tool = new PagingTool();
        //放入页码
        tool.setIndexNo(indexNo);
        //放入每页显示数量
        tool.setRows(rows);
        //放入角色集合
        tool.setList(list);
        //返回数据
        return new Result(new PaginationUtil<>(list.size(),rows, tool.getList(), indexNo));*/
        return new Result(list);
    }

    @Override
    public Result deleteOiCountdown(Integer id) {
        logger.info("删除倒计时事件");
        oiCountdownRepository.deleteById(id);
        return new Result();
    }

    @Override
    public Result OiCountdownTopping(Integer id, Integer classId) {
        logger.info("置顶倒计时事件");
        List<OiCountdown> list = new ArrayList<OiCountdown>();
        //查询置顶的信息
        OiCountdown oiCountdownOne = oiCountdownRepository.findByClassIdAndTopping(classId,1);
        //判断置顶的是否为空
        if(oiCountdownOne!=null){
            oiCountdownOne.setTopping(0);
            //添加到list
            list.add(oiCountdownOne);
        }
        //查询要置顶的事件倒计时
        OiCountdown oiCountdownTwo = oiCountdownRepository.findById(id).get();
        oiCountdownTwo.setTopping(1);
        list.add(oiCountdownTwo);
        oiCountdownRepository.saveAll(list);

        return new Result();
    }

    @Override
    public Result oiCountdownCancelTopping(Integer id) {

        //取消置顶
        OiCountdown oiCountdown = oiCountdownRepository.findById(id).get();
        oiCountdown.setTopping(0);
        oiCountdownRepository.save(oiCountdown);
        return new Result();
    }


}
